﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;

namespace WuxiWTO.Web.backend
{
    public partial class adminEdit : BasePage
    {
        int cid = 0;
        /// <summary>
        /// 更新模式
        /// </summary>
        protected bool UpdateMode
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    cid = int.Parse(Request.QueryString["id"]);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        WTODataContext contex = new WTODataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindGrid();
                if (UpdateMode)
                {
                    GetInfo();
                }
            }
        }

        private void GetInfo()
        {
            admin cat = contex.admins.SingleOrDefault(p => p.serial_number == cid);
            if (cat != null)
            {
                this.txtLoginName.Text = cat.admin_login_name;
                this.txtLoginName.Enabled = false;
                this.txtName.Text = cat.admin_name;
                this.txtCellphone.Text = cat.admin_mobile.ToString();
                this.txtTelephone.Text = cat.admin_telephone.ToString();
                this.txtPass.Text = cat.admin_password;
                this.txtAddress.Text = cat.admin_address;
                if (cat.admin_type != null)
                {
                    this.ddlType.Items.FindByValue(cat.admin_type.ToString()).Selected = true;
                }
                
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "fa", "alert('参数传递失败');", true);
            }
        }


        protected void btnOK_Click(object sender, EventArgs e)
        {
            admin link;
            if (UpdateMode)
            {
                link = contex.admins.SingleOrDefault(p => p.serial_number == cid);
                if (link == null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "fa", "alert('参数传递失败');", true);
                    return;
                }
            }
            else
                link = new admin();
            link.admin_login_name = this.txtLoginName.Text.Trim();
            link.admin_name = this.txtName.Text.Trim();
            link.admin_password = this.txtPass.Text.Trim();
            link.admin_telephone = this.txtTelephone.Text.Trim();
            link.admin_type = this.ddlType.SelectedItem.Value;
            link.admin_address = this.txtAddress.Text.Trim();
            link.admin_mobile = this.txtCellphone.Text.Trim();
            link.update_date = DateTime.Now;
            if (!UpdateMode)
            {
                link.created_date = DateTime.Now;
                contex.admins.InsertOnSubmit(link);
            }
            contex.SubmitChanges();
            Response.Redirect("adminList.aspx");
        }
    }
}